package com.yonyou.pmclouds.discuss.impl;

import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.discuss.entity.DiscussionVO;
import com.yonyou.pmclouds.discuss.itf.AbstractSpecifiedTypeDiscussProcessor;
import com.yonyou.pmclouds.safeinspection.entity.SafeInspectionVO;
import com.yonyou.pmclouds.safeinspection.rmiitf.SafeinspectionQueryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xu
 * @date 2020/5/1   20:51
 */
@Component
public class SafeInspectionDiscussProcessor extends AbstractSpecifiedTypeDiscussProcessor {

    private static final String MESSAGE_CREATOR = "了您发起的【安全巡查单】";
    private static final String MESSAGE_COMMENT = "了您评论的【安全巡查单】";
    private static final String MESSAGE_REPLY = "了您回复的【安全巡查单】";
    private static final String MESSAGE_AT = "在【安全巡查单】@了您";

    @Resource
    private SafeinspectionQueryService queryService;

    @Override
    protected Map<String, String> getUserTitle(DiscussionVO discussion) throws BusinessException {

        SafeInspectionVO inspectionVO = queryService.queryByOne(discussion.getRelateKey());
        //获取评论中被@的用户
        Map<String,String> result = new HashMap<>();
        String[] users = extractMentionedUser(discussion.getContent());
        //把被@人和@信息放到map集合中
        String atMessage = discussion.getCreatorName()+MESSAGE_AT;
        for (String user : users) {
            result.put(user, atMessage);
        }
        //判断discussion是评论还是回复
        int discussType = getDiscussType(discussion);
        //评论创建者和评论类型(是回复还是评论)
        String msgHead = discussion.getCreatorName() + getDiscussType(discussion);
        if (discussType == REPLY_TYPE){
            //评论类型是回复的情况
            // 回复，需要被回复人和评论人消息
            if (!StringUtils.isEmpty(discussion.getRepliedUser())){
                result.put(discussion.getRepliedUser(), msgHead + MESSAGE_REPLY);
            }
            //得到回复的评论
            DiscussionVO repliedDiscuss = getRepliedDiscuss(discussion);
            if (repliedDiscuss!=null){
                result.put(repliedDiscuss.getCreator(), msgHead + MESSAGE_COMMENT);
            }
        }

        result.put(inspectionVO.getCreator(), msgHead + MESSAGE_CREATOR);
        return result;
    }

    @Override
    public String getRelateType() {
        return BillTypeConst.SAFEINSPECTION_BILLTYPE;
    }
}
